<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤-计算属性</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<style>
    th,
    td {
        border: 1px solid black;
    }
</style>

<body>

    <div id="app">
        <h1>{{msg}}</h1>

        <input v-model="keyword" type="text" placeholder="请输入搜索关键字" /> <br /><br />

        <button @click="type=1">升序</button>
        <button @click="type=2">降序</button>
        <button @click="type=0">原序</button>
        <br /><br />


        <table>
            <tr>
                <th>序号</th>
                <th>英雄</th>
                <th>能量值</th>
                <th>选择</th>
            </tr>
            <tr v-for="(hero,index) in filterHeros" :key="hero.id">
                <td>{{index+1}}</td>
                <td>{{hero.name}}</td>
                <td>{{hero.power}}</td>
                <td><input type="checkbox" /></td>
            </tr>
        </table>


    </div>

    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                msg: '列表过滤-计算属性',
                keyword: '',
                type: 0,
                heros: [
                    { id: 101, name: '艾格文', power: 10000 },
                    { id: 102, name: '麦迪文', power: 19000 },
                    { id: 103, name: '古尔丹', power: 8000 },
                    { id: 104, name: '萨尔', power: 6000 },
                ]
            },
            computed: {
                filterHeros() {
                    //执行过滤规则
                    const arr = this.heros.filter((hero) => {
                        return hero.name.indexOf(this.keyword) >= 0
                    })

                    arr.sort((a, b) => {
                        if (this.type === 1) {
                            return a.power - b.power
                        } else if (this.type === 2) {
                            return b.power - a.power
                        }
                    })

                    //返回
                    return arr
                }
            }
        })


        // 回顾sort方法
        let arr = [8, 2, 1, 5, 6, 4, 3, 7]

        //sort方法排序后，不会生成一个新的数组，是在原数组的基础上进行排序，会影响原数组的结构
        arr.sort((a, b) => {
            return b - a
        })

        console.log(arr);

    </script>

</body>

</html>